package com.privatetime.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private File attachment;
	private String attachmentFileName;
	private String attachmentFileContentType;

	@Override
	public String execute() throws Exception {
		InputStream input = new FileInputStream(attachment);
		File parent = new File("e:/upload");
		parent.mkdir();
		OutputStream output = new FileOutputStream(new File(parent,
				attachmentFileName));

		byte[] buf = new byte[1024 * 10];
		int len = -1;
		while ((len = input.read(buf)) > 0) {
			output.write(buf, 0, len);
		}
		input.close();
		output.close();

		return ActionStateConstants.SUCCESS;
	}

	public File getAttachment() {
		return attachment;
	}

	public void setAttachment(File attachment) {
		this.attachment = attachment;
	}

	public String getAttachmentFileName() {
		return attachmentFileName;
	}

	public void setAttachmentFileName(String attachmentFileName) {
		this.attachmentFileName = attachmentFileName;
	}

	public String getAttachmentFileContentType() {
		return attachmentFileContentType;
	}

	public void setAttachmentFileContentType(String attachmentFileContentType) {
		this.attachmentFileContentType = attachmentFileContentType;
	}

	@Override
	public void validate() {
		if (attachment == null) {
			addFieldError("attachment", getText("select.upload.file"));
		}
	}

}
